package R.Dao;

import R.Entity.Diagnosis;
import R.Entity.Doctor;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

public class DoctorDao {
    public static Connection getConn() {
        Connection conn = null;
        try {
            Class.forName("com.mysql.jdbc.Driver");
            conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/hospital", "root", "123456");
        } catch (Exception e) {
            e.printStackTrace();
        }
        return conn;
    }

    public static List<Doctor> showAll() {
        List<Doctor> list = new ArrayList<>();
        try {
            Connection conn = RegDao.getConn();
            String sql="select doctor_number,department_name,doctor_name,doctor_gender,doctor_age,doctor_title from doctor";
            PreparedStatement ps = conn.prepareStatement(sql);
            ResultSet rs = ps.executeQuery();
            while (rs.next()) {
                Doctor d = new Doctor();
                String doctor_number = rs.getString(1);//医生编号
                String department_name = rs.getString(2);//科室名称
                String doctor_name = rs.getString(3);//医生姓名
                String doctor_gender = rs.getString(4);//医生性别
                String doctor_age = rs.getString(5);//医生年龄
                String doctor_title = rs.getString(6);//医生职称
                d.setDoctor_number(doctor_number);
                d.setDepartment_name(department_name);
                d.setDoctor_name(doctor_name);
                d.setDoctor_gender(doctor_gender);
                d.setDoctor_age(doctor_age);
                d.setDoctor_title(doctor_title);
                list.add(d);
            }
            conn.close();
            ps.close();
            rs.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return list;
    }

    public static List<Doctor> ChaXun(Doctor r) {
        List<Doctor> list = new ArrayList<>();
        try {
            Connection conn = RegDao.getConn();
            String sql="select doctor_number,department_name,doctor_name,doctor_gender,doctor_age,doctor_title from doctor where department_name='"+r.getDepartment_name()+"' and doctor_name='"+r.getDoctor_name()+"'";
            PreparedStatement ps = conn.prepareStatement(sql);
            ResultSet rs = ps.executeQuery();
            while (rs.next()) {
                Doctor d = new Doctor();
                String doctor_number = rs.getString(1);//医生编号
                String department_name = rs.getString(2);//科室名称
                String doctor_name = rs.getString(3);//医生姓名
                String doctor_gender = rs.getString(4);//医生性别
                String doctor_age = rs.getString(5);//医生年龄
                String doctor_title = rs.getString(6);//医生职称
                d.setDoctor_number(doctor_number);
                d.setDepartment_name(department_name);
                d.setDoctor_name(doctor_name);
                d.setDoctor_gender(doctor_gender);
                d.setDoctor_age(doctor_age);
                d.setDoctor_title(doctor_title);
                list.add(d);
            }
            conn.close();
            ps.close();
            rs.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return list;
    }
}
